﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ColorRichTextBox.ColorRichTextBox;
using GAM_Web_Designer_Pro.Properties;

namespace GAM_Web_Designer_Pro.Forms {
    public partial class NewFileForm : Form {

        // This is a list of all the templates.
        private SortedList<string, Template> m_templates;

        // This is a list of all the schemes.
        private SortedList<string, clsColorRichTextBoxSchema> m_schemas;

        // The parent form.
        private MainForm m_parent;

        /// <summary>
        /// Creates a NewFileForm.
        /// </summary>
        /// <param name="parent">The MDI parent.</param>
        public NewFileForm( MainForm parent ) {
            InitializeComponent();

            // Load all the templates and schemes for use in the form.
            m_templates = TemplateAccess.LoadAllTemplates();
            m_schemas = SchemaAccess.LoadAllSchemas();
            this.m_parent = parent;
        }

        /// <summary>
        /// Creates a NewFileForm
        /// </summary>
        /// <param name="parent">The MDI parent.</param>
        /// <param name="dirSelectVisable">Is the directory selection visible?</param>
        /// <param name="currentDir">The default starting directory.</param>
        public NewFileForm( MainForm parent, bool dirSelectVisable, string currentDir ) {
            InitializeComponent();

            m_templates = TemplateAccess.LoadAllTemplates();
            m_schemas = SchemaAccess.LoadAllSchemas();
            _directoryTB.Text = currentDir.Trim();
            dirPanel.Visible = dirSelectVisable;
            this.m_parent = parent;
        }

        private void NewFileForm_Load( object sender, EventArgs e ) {
            
            // Lets add each templates name into the _templateLB
            // so the user can select different templates.
            foreach ( Template temp in m_templates.Values )
                _templateLB.Items.Add( temp.Name );

            if ( _directoryTB.Text.Trim().Equals( string.Empty ) )
                _directoryTB.Text = Environment.GetEnvironmentVariable( "USERPROFILE" ) + "\\Documents\\GAM WD Projects";
        }

        private void browserBTN_Click( object sender, EventArgs e ) {
            if ( folderBrowserDialog1.ShowDialog() == DialogResult.OK )
                _directoryTB.Text = folderBrowserDialog1.SelectedPath;
        }

        private void CreateButton_Click( object sender, EventArgs e ) {

            // The first thing todo is check to make sure that the user has entered
            // all the information we need to create the project.
            if ( VerifyInfo() ) {

                try {
                    // This contains the path to the file the user is trying to create.
                    string savePath = _directoryTB.Text.Trim() + @"\" + _fileNameTB.Text.Trim();

                    // Now we create a stream writer so we can create the new file.
                    StreamWriter stream = File.CreateText( savePath );

                    // Now we create a template object for the file template that the user selected.
                    Template temp = m_templates.Values[ _templateLB.SelectedIndex ];

                    // We create a temporary scheme template to pass to the syntax highlighter,
                    // the syntax highlighter knows what to highlight. 
                    clsColorRichTextBoxSchema schema = new clsColorRichTextBoxSchema( m_schemas.Values[ m_schemas.IndexOfKey( temp.Language ) ] );
                    
                    // Write the file template's text into the new file and close the stream.
                    stream.Write( temp.Text );
                    stream.Close();

                    // Creates a new document form and set the current working path, the 
                    // syntax highlighter scheme, and the parent.
                    DocumentForm form = new DocumentForm( savePath, schema, m_parent );
                    form.Text = savePath;
                    form.MdiParent = m_parent;

                    // Now we add the new document form to the list of current forms and set is as active.
                    m_parent.CurrentForms.Add( form );
                    m_parent.ActiveChildForm = form;

                    // Finally show the new document form and hide this one.
                    form.Show();
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                catch ( Exception ex ) {
                    // Catch any exceptions and report them.
                    new BugReporter.BugReporterForm( Application.ProductName,
                      Application.ProductVersion, ex.ToString(), ex.Message ).Show();
                } 
            }
        }

        private void CancelButton_Click( object sender, EventArgs e ) {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void _templateLB_SelectedIndexChanged( object sender, EventArgs e ) {
            if ( _templateLB.SelectedIndex >= 0 && _templateLB.SelectedIndex < _templateLB.Items.Count ) {
                _fileNameTB.Text += m_schemas.Values[ m_schemas.IndexOfKey( m_templates.Values[ _templateLB.SelectedIndex ].Language ) ].ExtensionsSupported[ 0 ];
            }
        }

        private bool VerifyInfo() {
            if ( _fileNameTB.Text.Trim().Equals( string.Empty ) ) {
                MessageBox.Show( this, "You must enter a name for your file.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
                return false;
            }
            else if ( _directoryTB.Text.Trim().Equals( string.Empty ) ) {
                MessageBox.Show( this, "You select where you would like to place the file.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
                return false;
            }
            else if ( _templateLB.Text.Trim().Equals( string.Empty ) ) {
                MessageBox.Show( this, "You must a template for your file.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
                return false;
            }
            else
                return true;
        }
    }
}
